package com.dao;

import java.util.List;
import java.util.ArrayList;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;

import de.greenrobot.dao.AbstractDao;
import de.greenrobot.dao.Property;
import de.greenrobot.dao.internal.SqlUtils;
import de.greenrobot.dao.internal.DaoConfig;
import de.greenrobot.dao.query.Query;
import de.greenrobot.dao.query.QueryBuilder;

import com.model.Apiario;
import com.model.Apicultor;
import com.model.TiposIntervencao;

import com.model.Agenda;

// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT.
/** 
 * DAO for table AGENDA.
*/
public class AgendaDao extends AbstractDao<Agenda, Long> {

    public static final String TABLENAME = "AGENDA";

    /**
     * Properties of entity Agenda.<br/>
     * Can be used for QueryBuilder and for referencing column names.
    */
    public static class Properties {
        public final static Property Id = new Property(0, long.class, "id", true, "_idAgenda");
        public final static Property Grupoact = new Property(1, int.class, "grupoact", false, "GRUPOACT");
        public final static Property Data = new Property(2, int.class, "Data", false, "DATA");
        public final static Property Dia = new Property(3, int.class, "Dia", false, "DIA");
        public final static Property Mes = new Property(4, int.class, "Mes", false, "MES");
        public final static Property DataNormal = new Property(5, int.class, "DataNormal", false, "DATA_NORMAL");
        public final static Property Hora = new Property(6, java.util.Date.class, "Hora", false, "HORA");
        public final static Property Tarefa = new Property(7, String.class, "Tarefa", false, "TAREFA");
        public final static Property Observacoes = new Property(8, String.class, "Observacoes", false, "OBSERVACOES");
        public final static Property Concluido = new Property(9, boolean.class, "Concluido", false, "CONCLUIDO");
        public final static Property LembrarAntes1 = new Property(10, int.class, "LembrarAntes1", false, "LEMBRAR_ANTES1");
        public final static Property DataAlerta = new Property(11, java.util.Date.class, "DataAlerta", false, "DATA_ALERTA");
        public final static Property HoraAlerta = new Property(12, java.util.Date.class, "HoraAlerta", false, "HORA_ALERTA");
        public final static Property AlertaAutomatico = new Property(13, boolean.class, "AlertaAutomatico", false, "ALERTA_AUTOMATICO");
        public final static Property ZonaControlada = new Property(14, Boolean.class, "zonaControlada", false, "ZONA_CONTROLADA");
        public final static Property CultIntensa = new Property(15, Boolean.class, "cultIntensa", false, "CULT_INTENSA");
        public final static Property Transumanico = new Property(16, Boolean.class, "transumanico", false, "TRANSUMANICO");
        public final static Property Biologico = new Property(17, Boolean.class, "biologico", false, "BIOLOGICO");
        public final static Property Lab = new Property(18, Boolean.class, "lab", false, "LAB");
        public final static Property Desactivar = new Property(19, Boolean.class, "desactivar", false, "DESACTIVAR");
        public final static Property Nectar = new Property(20, Boolean.class, "nectar", false, "NECTAR");
        public final static Property Polen = new Property(21, Boolean.class, "polen", false, "POLEN");
        public final static Property Propolis = new Property(22, Boolean.class, "propolis", false, "PROPOLIS");
        public final static Property ApicultorId = new Property(23, long.class, "apicultorId", false, "APICULTOR_ID");
        public final static Property ApiarioId = new Property(24, long.class, "apiarioId", false, "APIARIO_ID");
        public final static Property TipoTarefaId = new Property(25, long.class, "tipoTarefaId", false, "TIPO_TAREFA_ID");
        public final static Property Date = new Property(26, java.util.Date.class, "date", false, "DATE");
    };

    private DaoSession daoSession;

    private Query<Agenda> apicultor_AgendaApicultorQuery;
    private Query<Agenda> apiario_AgendaApiarioQuery;
    private Query<Agenda> tiposIntervencao_AgendaTintervencaoQuery;

    public AgendaDao(DaoConfig config) {
        super(config);
    }
    
    public AgendaDao(DaoConfig config, DaoSession daoSession) {
        super(config, daoSession);
        this.daoSession = daoSession;
    }

    /** Creates the underlying database table. */
    public static void createTable(SQLiteDatabase db, boolean ifNotExists) {
        String constraint = ifNotExists? "IF NOT EXISTS ": "";
        db.execSQL("CREATE TABLE " + constraint + "'AGENDA' (" + //
                "'_idAgenda' INTEGER PRIMARY KEY ASC AUTOINCREMENT NOT NULL ," + // 0: id
                "'GRUPOACT' INTEGER NOT NULL ," + // 1: grupoact
                "'DATA' INTEGER NOT NULL ," + // 2: Data
                "'DIA' INTEGER NOT NULL ," + // 3: Dia
                "'MES' INTEGER NOT NULL ," + // 4: Mes
                "'DATA_NORMAL' INTEGER NOT NULL ," + // 5: DataNormal
                "'HORA' INTEGER NOT NULL ," + // 6: Hora
                "'TAREFA' TEXT NOT NULL ," + // 7: Tarefa
                "'OBSERVACOES' TEXT," + // 8: Observacoes
                "'CONCLUIDO' INTEGER NOT NULL ," + // 9: Concluido
                "'LEMBRAR_ANTES1' INTEGER NOT NULL ," + // 10: LembrarAntes1
                "'DATA_ALERTA' INTEGER NOT NULL ," + // 11: DataAlerta
                "'HORA_ALERTA' INTEGER NOT NULL ," + // 12: HoraAlerta
                "'ALERTA_AUTOMATICO' INTEGER NOT NULL ," + // 13: AlertaAutomatico
                "'ZONA_CONTROLADA' INTEGER," + // 14: zonaControlada
                "'CULT_INTENSA' INTEGER," + // 15: cultIntensa
                "'TRANSUMANICO' INTEGER," + // 16: transumanico
                "'BIOLOGICO' INTEGER," + // 17: biologico
                "'LAB' INTEGER," + // 18: lab
                "'DESACTIVAR' INTEGER," + // 19: desactivar
                "'NECTAR' INTEGER," + // 20: nectar
                "'POLEN' INTEGER," + // 21: polen
                "'PROPOLIS' INTEGER," + // 22: propolis
                "'APICULTOR_ID' INTEGER NOT NULL ," + // 23: apicultorId
                "'APIARIO_ID' INTEGER NOT NULL ," + // 24: apiarioId
                "'TIPO_TAREFA_ID' INTEGER NOT NULL ," + // 25: tipoTarefaId
                "'DATE' INTEGER);"); // 26: date
    }

    /** Drops the underlying database table. */
    public static void dropTable(SQLiteDatabase db, boolean ifExists) {
        String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "'AGENDA'";
        db.execSQL(sql);
    }

    /** @inheritdoc */
    @Override
    protected void bindValues(SQLiteStatement stmt, Agenda entity) {
        stmt.clearBindings();
        stmt.bindLong(1, entity.getId());
        stmt.bindLong(2, entity.getGrupoact());
        stmt.bindLong(3, entity.getData());
        stmt.bindLong(4, entity.getDia());
        stmt.bindLong(5, entity.getMes());
        stmt.bindLong(6, entity.getDataNormal());
        stmt.bindLong(7, entity.getHora().getTime());
        stmt.bindString(8, entity.getTarefa());
 
        String Observacoes = entity.getObservacoes();
        if (Observacoes != null) {
            stmt.bindString(9, Observacoes);
        }
        stmt.bindLong(10, entity.getConcluido() ? 1l: 0l);
        stmt.bindLong(11, entity.getLembrarAntes1());
        stmt.bindLong(12, entity.getDataAlerta().getTime());
        stmt.bindLong(13, entity.getHoraAlerta().getTime());
        stmt.bindLong(14, entity.getAlertaAutomatico() ? 1l: 0l);
 
        Boolean zonaControlada = entity.getZonaControlada();
        if (zonaControlada != null) {
            stmt.bindLong(15, zonaControlada ? 1l: 0l);
        }
 
        Boolean cultIntensa = entity.getCultIntensa();
        if (cultIntensa != null) {
            stmt.bindLong(16, cultIntensa ? 1l: 0l);
        }
 
        Boolean transumanico = entity.getTransumanico();
        if (transumanico != null) {
            stmt.bindLong(17, transumanico ? 1l: 0l);
        }
 
        Boolean biologico = entity.getBiologico();
        if (biologico != null) {
            stmt.bindLong(18, biologico ? 1l: 0l);
        }
 
        Boolean lab = entity.getLab();
        if (lab != null) {
            stmt.bindLong(19, lab ? 1l: 0l);
        }
 
        Boolean desactivar = entity.getDesactivar();
        if (desactivar != null) {
            stmt.bindLong(20, desactivar ? 1l: 0l);
        }
 
        Boolean nectar = entity.getNectar();
        if (nectar != null) {
            stmt.bindLong(21, nectar ? 1l: 0l);
        }
 
        Boolean polen = entity.getPolen();
        if (polen != null) {
            stmt.bindLong(22, polen ? 1l: 0l);
        }
 
        Boolean propolis = entity.getPropolis();
        if (propolis != null) {
            stmt.bindLong(23, propolis ? 1l: 0l);
        }
        stmt.bindLong(24, entity.getApicultorId());
        stmt.bindLong(25, entity.getApiarioId());
        stmt.bindLong(26, entity.getTipoTarefaId());
 
        java.util.Date date = entity.getDate();
        if (date != null) {
            stmt.bindLong(27, date.getTime());
        }
    }

    @Override
    protected void attachEntity(Agenda entity) {
        super.attachEntity(entity);
        entity.__setDaoSession(daoSession);
    }

    /** @inheritdoc */
    @Override
    public Long readKey(Cursor cursor, int offset) {
        return cursor.getLong(offset + 0);
    }    

    /** @inheritdoc */
    @Override
    public Agenda readEntity(Cursor cursor, int offset) {
        Agenda entity = new Agenda( //
            cursor.getLong(offset + 0), // id
            cursor.getInt(offset + 1), // grupoact
            cursor.getInt(offset + 2), // Data
            cursor.getInt(offset + 3), // Dia
            cursor.getInt(offset + 4), // Mes
            cursor.getInt(offset + 5), // DataNormal
            new java.util.Date(cursor.getLong(offset + 6)), // Hora
            cursor.getString(offset + 7), // Tarefa
            cursor.isNull(offset + 8) ? null : cursor.getString(offset + 8), // Observacoes
            cursor.getShort(offset + 9) != 0, // Concluido
            cursor.getInt(offset + 10), // LembrarAntes1
            new java.util.Date(cursor.getLong(offset + 11)), // DataAlerta
            new java.util.Date(cursor.getLong(offset + 12)), // HoraAlerta
            cursor.getShort(offset + 13) != 0, // AlertaAutomatico
            cursor.isNull(offset + 14) ? null : cursor.getShort(offset + 14) != 0, // zonaControlada
            cursor.isNull(offset + 15) ? null : cursor.getShort(offset + 15) != 0, // cultIntensa
            cursor.isNull(offset + 16) ? null : cursor.getShort(offset + 16) != 0, // transumanico
            cursor.isNull(offset + 17) ? null : cursor.getShort(offset + 17) != 0, // biologico
            cursor.isNull(offset + 18) ? null : cursor.getShort(offset + 18) != 0, // lab
            cursor.isNull(offset + 19) ? null : cursor.getShort(offset + 19) != 0, // desactivar
            cursor.isNull(offset + 20) ? null : cursor.getShort(offset + 20) != 0, // nectar
            cursor.isNull(offset + 21) ? null : cursor.getShort(offset + 21) != 0, // polen
            cursor.isNull(offset + 22) ? null : cursor.getShort(offset + 22) != 0, // propolis
            cursor.getLong(offset + 23), // apicultorId
            cursor.getLong(offset + 24), // apiarioId
            cursor.getLong(offset + 25), // tipoTarefaId
            cursor.isNull(offset + 26) ? null : new java.util.Date(cursor.getLong(offset + 26)) // date
        );
        return entity;
    }
     
    /** @inheritdoc */
    @Override
    public void readEntity(Cursor cursor, Agenda entity, int offset) {
        entity.setId(cursor.getLong(offset + 0));
        entity.setGrupoact(cursor.getInt(offset + 1));
        entity.setData(cursor.getInt(offset + 2));
        entity.setDia(cursor.getInt(offset + 3));
        entity.setMes(cursor.getInt(offset + 4));
        entity.setDataNormal(cursor.getInt(offset + 5));
        entity.setHora(new java.util.Date(cursor.getLong(offset + 6)));
        entity.setTarefa(cursor.getString(offset + 7));
        entity.setObservacoes(cursor.isNull(offset + 8) ? null : cursor.getString(offset + 8));
        entity.setConcluido(cursor.getShort(offset + 9) != 0);
        entity.setLembrarAntes1(cursor.getInt(offset + 10));
        entity.setDataAlerta(new java.util.Date(cursor.getLong(offset + 11)));
        entity.setHoraAlerta(new java.util.Date(cursor.getLong(offset + 12)));
        entity.setAlertaAutomatico(cursor.getShort(offset + 13) != 0);
        entity.setZonaControlada(cursor.isNull(offset + 14) ? null : cursor.getShort(offset + 14) != 0);
        entity.setCultIntensa(cursor.isNull(offset + 15) ? null : cursor.getShort(offset + 15) != 0);
        entity.setTransumanico(cursor.isNull(offset + 16) ? null : cursor.getShort(offset + 16) != 0);
        entity.setBiologico(cursor.isNull(offset + 17) ? null : cursor.getShort(offset + 17) != 0);
        entity.setLab(cursor.isNull(offset + 18) ? null : cursor.getShort(offset + 18) != 0);
        entity.setDesactivar(cursor.isNull(offset + 19) ? null : cursor.getShort(offset + 19) != 0);
        entity.setNectar(cursor.isNull(offset + 20) ? null : cursor.getShort(offset + 20) != 0);
        entity.setPolen(cursor.isNull(offset + 21) ? null : cursor.getShort(offset + 21) != 0);
        entity.setPropolis(cursor.isNull(offset + 22) ? null : cursor.getShort(offset + 22) != 0);
        entity.setApicultorId(cursor.getLong(offset + 23));
        entity.setApiarioId(cursor.getLong(offset + 24));
        entity.setTipoTarefaId(cursor.getLong(offset + 25));
        entity.setDate(cursor.isNull(offset + 26) ? null : new java.util.Date(cursor.getLong(offset + 26)));
     }
    
    /** @inheritdoc */
    @Override
    protected Long updateKeyAfterInsert(Agenda entity, long rowId) {
        entity.setId(rowId);
        return rowId;
    }
    
    /** @inheritdoc */
    @Override
    public Long getKey(Agenda entity) {
        if(entity != null) {
            return entity.getId();
        } else {
            return null;
        }
    }

    /** @inheritdoc */
    @Override    
    protected boolean isEntityUpdateable() {
        return true;
    }
    
    /** Internal query to resolve the "agendaApicultor" to-many relationship of Apicultor. */
    public List<Agenda> _queryApicultor_AgendaApicultor(long apicultorId) {
        synchronized (this) {
            if (apicultor_AgendaApicultorQuery == null) {
                QueryBuilder<Agenda> queryBuilder = queryBuilder();
                queryBuilder.where(Properties.ApicultorId.eq(null));
                queryBuilder.orderRaw("DATE ASC");
                apicultor_AgendaApicultorQuery = queryBuilder.build();
            }
        }
        Query<Agenda> query = apicultor_AgendaApicultorQuery.forCurrentThread();
        query.setParameter(0, apicultorId);
        return query.list();
    }

    /** Internal query to resolve the "agendaApiario" to-many relationship of Apiario. */
    public List<Agenda> _queryApiario_AgendaApiario(long apiarioId) {
        synchronized (this) {
            if (apiario_AgendaApiarioQuery == null) {
                QueryBuilder<Agenda> queryBuilder = queryBuilder();
                queryBuilder.where(Properties.ApiarioId.eq(null));
                queryBuilder.orderRaw("DATE ASC");
                apiario_AgendaApiarioQuery = queryBuilder.build();
            }
        }
        Query<Agenda> query = apiario_AgendaApiarioQuery.forCurrentThread();
        query.setParameter(0, apiarioId);
        return query.list();
    }

    /** Internal query to resolve the "agendaTintervencao" to-many relationship of TiposIntervencao. */
    public List<Agenda> _queryTiposIntervencao_AgendaTintervencao(long tipoTarefaId) {
        synchronized (this) {
            if (tiposIntervencao_AgendaTintervencaoQuery == null) {
                QueryBuilder<Agenda> queryBuilder = queryBuilder();
                queryBuilder.where(Properties.TipoTarefaId.eq(null));
                queryBuilder.orderRaw("DATE ASC");
                tiposIntervencao_AgendaTintervencaoQuery = queryBuilder.build();
            }
        }
        Query<Agenda> query = tiposIntervencao_AgendaTintervencaoQuery.forCurrentThread();
        query.setParameter(0, tipoTarefaId);
        return query.list();
    }

    private String selectDeep;

    protected String getSelectDeep() {
        if (selectDeep == null) {
            StringBuilder builder = new StringBuilder("SELECT ");
            SqlUtils.appendColumns(builder, "T", getAllColumns());
            builder.append(',');
            SqlUtils.appendColumns(builder, "T0", daoSession.getApicultorDao().getAllColumns());
            builder.append(',');
            SqlUtils.appendColumns(builder, "T1", daoSession.getApiarioDao().getAllColumns());
            builder.append(',');
            SqlUtils.appendColumns(builder, "T2", daoSession.getTiposIntervencaoDao().getAllColumns());
            builder.append(" FROM AGENDA T");
            builder.append(" LEFT JOIN APICULTOR T0 ON T.'APICULTOR_ID'=T0.'_idApicultor'");
            builder.append(" LEFT JOIN APIARIO T1 ON T.'APIARIO_ID'=T1.'_idApiario'");
            builder.append(" LEFT JOIN TIPOS_INTERVENCAO T2 ON T.'TIPO_TAREFA_ID'=T2.'_idTiposIntervencao'");
            builder.append(' ');
            selectDeep = builder.toString();
        }
        return selectDeep;
    }
    
    protected Agenda loadCurrentDeep(Cursor cursor, boolean lock) {
        Agenda entity = loadCurrent(cursor, 0, lock);
        int offset = getAllColumns().length;

        Apicultor apicultor = loadCurrentOther(daoSession.getApicultorDao(), cursor, offset);
         if(apicultor != null) {
            entity.setApicultor(apicultor);
        }
        offset += daoSession.getApicultorDao().getAllColumns().length;

        Apiario apiario = loadCurrentOther(daoSession.getApiarioDao(), cursor, offset);
         if(apiario != null) {
            entity.setApiario(apiario);
        }
        offset += daoSession.getApiarioDao().getAllColumns().length;

        TiposIntervencao tiposIntervencao = loadCurrentOther(daoSession.getTiposIntervencaoDao(), cursor, offset);
         if(tiposIntervencao != null) {
            entity.setTiposIntervencao(tiposIntervencao);
        }

        return entity;    
    }

    public Agenda loadDeep(Long key) {
        assertSinglePk();
        if (key == null) {
            return null;
        }

        StringBuilder builder = new StringBuilder(getSelectDeep());
        builder.append("WHERE ");
        SqlUtils.appendColumnsEqValue(builder, "T", getPkColumns());
        String sql = builder.toString();
        
        String[] keyArray = new String[] { key.toString() };
        Cursor cursor = db.rawQuery(sql, keyArray);
        
        try {
            boolean available = cursor.moveToFirst();
            if (!available) {
                return null;
            } else if (!cursor.isLast()) {
                throw new IllegalStateException("Expected unique result, but count was " + cursor.getCount());
            }
            return loadCurrentDeep(cursor, true);
        } finally {
            cursor.close();
        }
    }
    
    /** Reads all available rows from the given cursor and returns a list of new ImageTO objects. */
    public List<Agenda> loadAllDeepFromCursor(Cursor cursor) {
        int count = cursor.getCount();
        List<Agenda> list = new ArrayList<Agenda>(count);
        
        if (cursor.moveToFirst()) {
            if (identityScope != null) {
                identityScope.lock();
                identityScope.reserveRoom(count);
            }
            try {
                do {
                    list.add(loadCurrentDeep(cursor, false));
                } while (cursor.moveToNext());
            } finally {
                if (identityScope != null) {
                    identityScope.unlock();
                }
            }
        }
        return list;
    }
    
    protected List<Agenda> loadDeepAllAndCloseCursor(Cursor cursor) {
        try {
            return loadAllDeepFromCursor(cursor);
        } finally {
            cursor.close();
        }
    }
    

    /** A raw-style query where you can pass any WHERE clause and arguments. */
    public List<Agenda> queryDeep(String where, String... selectionArg) {
        Cursor cursor = db.rawQuery(getSelectDeep() + where, selectionArg);
        return loadDeepAllAndCloseCursor(cursor);
    }
 
}
